using System;
using System.Windows.Input;

namespace AppTouchScreen.Helper
{
    internal class BaseCommand : ICommand
    {
        private readonly Action _command;

        private readonly Action<object> _commandWithParametr;

        private readonly Func<bool> _canExecute;

        private readonly Func<object, bool> _canExecuteWithParametr;

        //private object parametr = null;

        public BaseCommand(Action command, Func<bool> canExecute = null)
        {
            if (command == null)
                throw new ArgumentNullException("command");
            _canExecute = canExecute;
            _command = command;
        }

        public BaseCommand(Action<object> command, Func<bool> canExecute = null)
        {
            if (command == null)
                throw new ArgumentNullException("command");
            _canExecute = canExecute;
            _commandWithParametr = command;
        }

        public BaseCommand(Action<object> command, Func<object, bool> canExecute = null)
        {
            if (command == null)
                throw new ArgumentNullException("command");
            _canExecuteWithParametr = canExecute;
            _commandWithParametr = command;
        }

        public void Execute(object parameter)
        {
            if (parameter == null)
            {
                _command();
            }
            else
            {
                _commandWithParametr(parameter);
            }
        }

        public bool CanExecute(object parameter)
        {
            //bool result = true;

            if ((_canExecute == null)&&(_canExecuteWithParametr == null))
            {
                return true;
            }
            //if (_canExecute == null)
            //    return true;
            else
            {
                if (parameter!=null)
                {
                    if (_canExecute != null)
                    {
                        return _canExecute();
                    }
                    else
                    {
                        return _canExecuteWithParametr(parameter);
                    }
                }
                else
                {
                    return true;
                }

            }

            //return result;

            //return _canExecute();
        }


        public event EventHandler CanExecuteChanged;
    }
}